package com.lpx.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lpx.entity.Users;
import com.lpx.mapper.UserMapper;

@Service
public class MyUserDetailsService implements UserDetailsService {

	@Autowired
	private  UserMapper userMapper;
	
	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

		List<GrantedAuthority> auList = AuthorityUtils.commaSeparatedStringToAuthorityList("manger,ROLE_teacher");
		
		LambdaQueryWrapper<Users> queryWrapper = new LambdaQueryWrapper<Users>();
		queryWrapper.eq(Users::getUsername,username);//使用 User::getUaerame 来传递对象属性名 ，
		Users user = userMapper.selectOne(queryWrapper);
		if(user != null) {
			return new User(user.getUsername(),new BCryptPasswordEncoder().encode(user.getPassword()),auList);
		}else {
			throw new  UsernameNotFoundException("用户名不存在");
		}
		
	}

}
